/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package RepositoryHarvester;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Properties;

/**
 *
 * @author Lawrence
 */
public class HarvestConfiguration
{
    /** The name of the harvester settings file (different from the repository ones) */  
    public final static String settingsFile = "config/DatabaseConfig.xml";
    /** the name of the configuration file that is being used*/
    private final String harvestFileName = "config/HarvestConfig.xml";

    /* Instance variables */    
     /** A boolean to check if this config file has correctly started up.
    * True if all is good and you may use it. False if there is some erro and you should abort*/
    public boolean isLoaded;
    /**  url to harvest from */
    private String baseURL;
    /** the metadata format to harvest */
    private String metadataFormat;
    /** last date harvesed */
    private String dateFrom;
    /** the URL of the database to harvest to */
    private String databaseURL;
    /** the username to connect to the database with  */
    private String databaseUser;
    /** the plaintext password for the database */
    private String databasePassword;
    /** the time between harvests (milliseconds)*/
    private long harvestInterval;
  
    /**
    * Constructor to set up the instance variables and load up the harverst and database
    * properties
    * @param target the file name of the repository configuration that is to be harvested
    */
    public HarvestConfiguration(){ // create an instance of this object that will store the configuration of the harvest specified in target
        
        isLoaded = true;
        loadHarvestProperties(); // load the harvest properties
        loadDatabaseProperties(); // load the database properties
    }

    /**
   * Loads the <code>xml</code> config file representing the repository settings
   * using a <code>Properties</code> object. Stores them in local private variables.
   */
  private void loadHarvestProperties(){
     Properties harvestSettings = new Properties(); // Properties object that will be used to load the harvest settings (source, metadataformats)
     FileInputStream harvestSettingsFileStream = null;

     try {
       harvestSettingsFileStream = new FileInputStream(harvestFileName); // create inputstream for settingsFile
     } catch (Exception e){ // if there is an error loading the settingsFile, exit and print a message
	System.out.println("Could not load " + harvestFileName + ". Loading Defaults."); // error message
	//System.out.println("Please make sure that the file exists and you are allowed access to it.");
	// we should now cancel this specific harvest
        isLoaded = false;
     }

     try {
	harvestSettings.loadFromXML(harvestSettingsFileStream); // try load the database settings from the file input stream

	/* Load all the values from the file */
	baseURL = harvestSettings.getProperty("baseURL");
	metadataFormat = harvestSettings.getProperty("metadataFormat");
	dateFrom = harvestSettings.getProperty("dateFrom");	
	harvestInterval = Long.decode( harvestSettings.getProperty("harvestInterval") );	        

	harvestSettingsFileStream.close(); // close the file stream
     } catch (Exception e){ // if there is an error
	System.out.println("Could not load harvest configuration from " + harvestFileName + "."); //error message
	System.out.println("Please make sure that the file is correctly formatted.");
	// we should now cancel this specific harvest
        isLoaded = false;
     }
  }

  /**
   * Method to load the database properties from the configuration file settings file
   */
  private void loadDatabaseProperties(){

     Properties databaseSettings = new Properties(); // Properties object that will be used to load the database settings
     FileInputStream dbSettingsFileStream = null;

     try {
       dbSettingsFileStream = new FileInputStream(settingsFile); // create inputstream for settingsFile
     } catch (Exception e){ // if there is an error loading the settingsFile, exit and print a message
	System.out.println("Could not load database configuration from " + settingsFile + "."); // error message
	System.out.println("Please make sure that the file exists and you are allowed access to it.");
        databaseURL = "jdbc:mysql://blue.cs.uct.ac.za:3306/wwwproject";
        databaseUser = "grapher";
        databasePassword = "password";
	isLoaded = false;
     }

     try {
	databaseSettings.loadFromXML(dbSettingsFileStream); // try load the database settings from the file input stream

	/* Load all the values from the file */
	databaseURL = databaseSettings.getProperty("databaseURL");
	databaseUser = databaseSettings.getProperty("databaseUsername");
	databasePassword = databaseSettings.getProperty("databasePassword");

        dbSettingsFileStream.close(); // close the file stream
     } catch (Exception e){ // if there is an error
	System.out.println("Could not load database configuration from " + settingsFile + "."); //error message
	System.out.println("Please make sure that the file is correctly formatted.");
	isLoaded = false;
     }


  }

  /**
   * Save the harvest configuration back to the original file
   */
  private void saveHarvestConfiguration()
  {
        Properties harvestSettings = new Properties (); // create the properties object that will be saved to file

        /* set the properties in the harvestSettings object to the values stored in this configuration object */
        harvestSettings.setProperty("baseURL",baseURL);
        harvestSettings.setProperty("metadataFormat",metadataFormat);
        if (dateFrom != null) harvestSettings.setProperty("dateFrom",dateFrom); // if there is a dateFrom we add it
        harvestSettings.setProperty("harvestInterval",String.valueOf(harvestInterval));
	try {
		FileOutputStream out = new FileOutputStream(harvestFileName); // create the file output stream
		harvestSettings.storeToXML(out, "OAI Metadata Harvester Settings File ");
		out.close();
	} catch (Exception e){ // if there is an error
		System.out.println("Unable to save harvest settings file " + harvestFileName + ".");
		System.out.println("The web interface data may now be out of date.");
	}

  }

  /* Accessor and Mutator methods */


  /**
   * Updates the <code>dateFrom</code> in the repository config file to the current
   * date and time.
   */
  public void updateDateFrom(){ // update the dateFrom in the properties and save the file
	Timestamp currentDate = new Timestamp( ( new Date() ).getTime() ); // get the current timestamp
	dateFrom = currentDate.toString();	// convert to a string
	saveHarvestConfiguration(); // save this to the file
  }  

  /**
   * Accessor method to get the url of the repository to be harvested.
   * @return the base Url of the repository to be harvested.
   */
  public String getBaseURL(){
	return baseURL;
  }

/**
 * Accessor method to get the type of metadata that is to be harvested.
 * @return the metadata format that is to be harvested
 */
  public String getMetadataFormat(){
	return metadataFormat;
  }

  /**
   * Accessor method to get the date of the last harvest.
   * @return the last date harvested
   */
  public String getDateFrom(){
	return dateFrom;
  }


  /**
   * Gets the url of the database in which we will store the records.
   * @return the database url.
   */
  public String getDatabaseURL(){
	return databaseURL;
  }

  /**
   * Gets the database username
   * @return the username used to log in to the database
   */
  public String getDatabaseUser(){
	return databaseUser;
  }

  /**
   * Gets the database password
   * @return the password corresponding the username used to log in to the database
   */
  public String getDatabasePassword(){
	return databasePassword;
  }

  /**
   * Gets the interval time between harvests.
   * @return the interval time between harvests.
   */
  public long getHarvestInterval(){
	return harvestInterval;
  }

}
